/* $Id: comment.js 2010-08-31 16:02 idris $ */

en4.core.comments = {};

en4.core.comments = {
    
  $element: {},
		
  loadComments : function(type, id, page){
    var bind = this;
    en4.core.request.send(new Request.HTML({
      url : en4.core.baseUrl + 'page-comment/list',
      data : {
        format : 'html',
        type : type,
        id : id,
        page : page
      }
    }), {
      'element' : bind.$element
    });
  },

  attachCreateComment : function(formElement){
    var bind = this;
    formElement.addEvent('submit', function(event){
      event.stop();
      var form_values  = formElement.toQueryString();
          form_values += '&format=json';
          form_values += '&id='+formElement.identity.value;
      en4.core.request.send(new Request.JSON({
        url : en4.core.baseUrl + 'page-comment/create',
        data : form_values
      }), {
        'element' : bind.$element
      });
      //bind.comment(formElement.type.value, formElement.identity.value, formElement.body.value);
    })
  },

  comment : function(type, id, body){
    var bind = this;
    en4.core.request.send(new Request.JSON({
      url : en4.core.baseUrl + 'page-comment/create',
      data : {
        format : 'json',
        type : type,
        id : id,
        body : body
      }
    }), {
      'element' : bind.$element
    });
  },

  like : function(type, id){
    var bind = this;
    en4.core.request.send(new Request.JSON({
      url : en4.core.baseUrl + 'page-comment/like',
      data : {
        format : 'json',
        type : type,
        id : id
      }
    }), {
      'element' : bind.$element
    });
  },

  unlike : function(type, id){
    var bind = this;
    en4.core.request.send(new Request.JSON({
      url : en4.core.baseUrl + 'page-comment/unlike',
      data : {
        format : 'json',
        type : type,
        id : id
      }
    }), {
      'element' : bind.$element
    });
  },

  showLikes : function(type, id){
    var bind = this;
    en4.core.request.send(new Request.HTML({
      url : en4.core.baseUrl + 'page-comment/list',
      data : {
        format : 'html',
        type : type,
        id : id,
        viewAllLikes : true
      }
    }), {
      'element' : bind.$element
    });
  },
  
  deleteComment : function(type, id, comment_id) {
    (new Request.JSON({
      url : en4.core.baseUrl + 'page-comment/delete',
      data : {
        format : 'json',
        type : type,
        id : id,
        comment_id : comment_id
      },
      onComplete: function() {
        if( $('comment-' + comment_id) ) {
          $('comment-' + comment_id).destroy();
        }
        try {
          var commentCount = $$('.comments_options span')[0];
          var m = commentCount.get('html').match(/\d+/);
          var newCount = ( parseInt(m[0]) != 'NaN' && parseInt(m[0]) > 1 ? parseInt(m[0]) - 1 : 0 );
          commentCount.set('html', commentCount.get('html').replace(m[0], newCount));
        } catch( e ) {}
      }
    })).send();
  }

};